<template>
  <view class="container">
    <button @click="chooseAndPreviewImage">选择并预览图片</button>
    <view class="image-list">
      <image 
        v-for="(img, index) in imagePaths" 
        :key="index" 
        :src="img" 
        mode="aspectFit" 
        class="image-item"
      ></image>
    </view>
  </view>
   <view class="weather-container">
      <view v-if="weatherData" class="weather-info">
        <view class="date">{{ tomorrowDate }} {{ tomorrowWeek }}</view>
        <view class="temp">最高温：{{ tomorrowMax }}°C</view>
        <view class="temp">最低温：{{ tomorrowMin }}°C</view>
      </view>
      <view v-else class="loading">加载中...</view>
    </view>
	 <view class="image-container">
	    <button @click="downloadImage">下载并显示图片</button>
	    <image v-if="imagePath" :src="imagePath" mode="aspectFit" class="image"></image>
	    <view v-else class="tip">点击按钮下载图片</view>
	  </view>
</template>

<script>
export default {
  data() {
    return {
      imagePaths: [],
	      weatherData: null,
	        tomorrowDate: '',
	        tomorrowWeek: '',
	        tomorrowMax: '',
	        tomorrowMin: '',
			  imagePath: '', // 存储下载后图片的本地路径
			      imageUrl: 'https://cdn.pixabay.com/photo/2025/11/05/20/57/monastery-9939590_1280.jpg' // 网络图片地址
    };
  },
   onLoad() {
      this.getWeatherInfo();
    },
  methods: {
    // 选择图片并预览
    chooseAndPreviewImage() {
      uni.chooseImage({
        count: 5, // 最多选择5张图片
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'], // 可从相册选择或相机拍摄
        success: (res) => {
          const tempFilePaths = res.tempFilePaths;
          // 存储图片路径，用于界面显示
          this.imagePaths = tempFilePaths;
          // 预览图片
          uni.previewImage({
            current: tempFilePaths[0], // 初始显示的图片路径
            urls: tempFilePaths, // 需要预览的图片路径数组
            success: (e) => {
              console.log('预览图片成功');
            }
          });
        }
      });
    },
	  getWeatherInfo() {
	      uni.request({
	        url: 'http://t.weather.sojson.com/api/weather/city/101230501',
	        method: 'GET',
	        success: (res) => {
	          if (res.statusCode === 200 && res.data.status === 200) {
	            this.weatherData = res.data;
	            // 提取明天的天气预报（数组中第2个元素为明天）
	            const tomorrowWeather = res.data.data.forecast[1];
	            this.tomorrowDate = tomorrowWeather.date;
	            this.tomorrowWeek = tomorrowWeather.week;
	            this.tomorrowMax = tomorrowWeather.high.replace('最高', '');
	            this.tomorrowMin = tomorrowWeather.low.replace('最低', '');
	          }
	        },
	        fail: (err) => {
	          console.error('获取天气失败：', err);
	          uni.showToast({
	            title: '获取天气失败',
	            icon: 'none'
	          });
	        }
	      });
	    },
		   downloadImage() {
		      uni.showLoading({
		        title: '下载中...'
		      });
		      uni.downloadFile({
		        url: this.imageUrl,
		        success: (downloadRes) => {
		          if (downloadRes.statusCode === 200) {
		            // 下载成功，获取临时文件路径并显示
		            this.imagePath = downloadRes.tempFilePath;
		            uni.hideLoading();
		            uni.showToast({
		              title: '下载成功',
		              icon: 'success'
		            });
		          }
		        },
		        fail: (err) => {
		          uni.hideLoading();
		          uni.showToast({
		            title: '下载失败',
		            icon: 'none'
		          });
		          console.error('图片下载失败：', err);
		        },
		        progress: (progressRes) => {
		          // 打印下载进度
		          console.log(`下载进度：${progressRes.progress}%`);
		          console.log(`已下载长度：${progressRes.totalBytesWritten} 字节`);
		          console.log(`总长度：${progressRes.totalBytesExpectedToWrite} 字节`);
		        }
		      });
		    }
  }
};
</script>

<style scoped>
.container {
  padding: 20rpx;
}
.image-list {
  display: flex;
  flex-wrap: wrap;
  margin-top: 20rpx;
}
.image-item {
  width: 200rpx;
  height: 200rpx;
  margin: 10rpx;
  border: 1rpx solid #eee;
}
</style>